package chatwriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.output.FileWriterWithEncoding;
import org.joda.time.DateTime;

//simple class to write test chat files
public class ChatWriter {

	public ChatWriter() throws IOException, InterruptedException {
		Path logPath = Paths.get(System.getProperty("user.home"), "Documents", "EVE", "logs", "Testlogs");
		DateTime date = new DateTime();
		
		Path inputFilePath = Paths.get(System.getProperty("user.home"), "Documents", "EVE", "logs", "Testlogs", "Help_20130525_010742.txt");
		Scanner scanner = new Scanner(inputFilePath);
		
		//create a new file in the same format as the Eve Chat Logs
		Path path = Paths.get(logPath.toString(), "Test_" + date.getYear() + date.getMonthOfYear() + date.getDayOfMonth() + "_" + date.getHourOfDay() + date.getMinuteOfHour() + date.getSecondOfMinute() + ".txt");
		String border = "---------------------------------------------------------------\n";
		String month = date.getMonthOfYear() < 10 ? "0" + date.getMonthOfYear() : "" + date.getMonthOfYear();
		String day = date.getDayOfMonth() < 10 ? "0" + date.getDayOfMonth() : "" + date.getDayOfMonth();
		String hour = date.getHourOfDay() < 10 ? "0" + date.getHourOfDay() : "" + date.getHourOfDay();
		String minute = date.getMinuteOfHour() < 10 ? "0" + date.getMinuteOfHour() : "" + date.getMinuteOfHour();
		String second = date.getSecondOfMinute() < 10 ? "0" + date.getSecondOfMinute() : "" + date.getSecondOfMinute();
		
		String timeString = "  Session started: " + date.getYear() + "." + month + "." + day + " " + hour + ":" + minute + ":" + second;
		String header = "\n\n\n       \n" + border + "\n  Channel ID:      (('corpid', 000999999),)\n  Channel Name:    Corp\n  Listener:        Test Moonstrider\n" + timeString + "\n" + border + "\n";
//		Files.write(path, header.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
//		FileWriterWithEncoding fw = new FileWriterWithEncoding(new File(path.toString()), Charsets.UTF_16);
//		fw.write(header);
		
		String sampleText = "";
		double count = 0.0;
		while (scanner.hasNext()) {
//			DateTime cd = new DateTime();
//			String mon = cd.getMonthOfYear() < 10 ? "0" + cd.getMonthOfYear() : "" + cd.getMonthOfYear();
//			String d = cd.getDayOfMonth() < 10 ? "0" + cd.getDayOfMonth() : "" + cd.getDayOfMonth();
//			String h = cd.getHourOfDay() < 10 ? "0" + cd.getHourOfDay() : "" + cd.getHourOfDay();
//			String min = cd.getMinuteOfHour() < 10 ? "0" + cd.getMinuteOfHour() : "" + cd.getMinuteOfHour();
//			String sec = cd.getSecondOfMinute() < 10 ? "0" + cd.getSecondOfMinute() : "" + cd.getSecondOfMinute();
//			String ts = "[  " + cd.getYear() + "." + mon + "." + d + " " + h + ":" + min + ":" + sec + "  ]";
//			sampleText = ts + " Test Chat Line " + count++ + "\n";
//			fw.write(sampleText);
//			Thread.sleep(1000);
			String ln = scanner.nextLine() + scanner.delimiter();
			Files.write(path, ln.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
		}
//		fw.close();
	}
		
	
	public static void main(String[] args) throws IOException, InterruptedException {
		new ChatWriter();
	}
}
